<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/14
 * Time: 13:51
 */
namespace haibao\jiepai\business;

class Advert extends Base
{
    /**
     * @var $_instance
     */
    private static $_instance;

    /**
     * type = 1~6 为首页置顶位广告，7~8为信息流广告
     * @var array
     */
    private static $adIdArray = array(1013, 1014, 1015, 1016, 1017, 1018, 1019, 1027);

    /**
     * 9~为视频广告
     * @var array
     */
    private static $adIdArrayOfVideo = array(1046);

    /**
     * instance()
     */
    private static function instance()
    {
        if (!self::$_instance) {
            self::$_instance = new \haibao\jiepai\data\Advert();
        }
        return self::$_instance;
    }

    public static function getAdvertList()
    {
        $retAdvert = array();
        $advertList = self::instance()->getAdvertList(self::$adIdArray);
        if ($advertList) {
            foreach ($advertList as $key => $advert) {
                if ($advert['weight'] == 0) {
                    unset($advertList[$key]);
                    continue;
                }
                $adv = array();
                $adv['adId'] = $advert['interface_banner_id'];
                $adv['zoneId'] = $advert['zone_id'];
                $adv['picUrl'] = 'http://cdn0.haibao.cn/store/media/' . $advert['filename'];
                preg_match_all('/href=\"javascript:;\">(.*)<\/a>/isU', $advert['bannertext'], $result);
                $adv['title'] = join('###', $result[1]);
                if ($advert['url'] == 'http://') {
                    $adv['adUrl'] = '';
                } else {
                    $adv['adUrl'] = self::getAdUrl($advert['interface_banner_id'], $advert['zone_id'], $advert['url']);
                }
                if ($advert['pv_detection']) {
                    preg_match('/<img src=\"(.*)\" width=0/isU', $advert['pv_detection'], $result);
                    $adv['pvMonitor'] = $result[1];
                } else {
                    $adv['pvMonitor'] = null;
                }
                $adv['bmsPvMonitor'] = self::getBmsPvMonitor($advert['interface_banner_id'], $advert['interface_campaign_id'], $advert['zone_id']);
                if (isset($advert['color'])) {
                    $adv['color'] = $advert['color'];
                }
                $adv['type'] = $advert['type'];
                array_push($retAdvert, $adv);
            }
        }
        return $retAdvert;
    }

    /**
     * 获取文章列表页视频广告
     * @return array
     */
    public static function getVideoAdvert()
    {
        $retAdvert = array();
        $videoList = self::instance()->getVideoAdvertList(self::$adIdArrayOfVideo);
        if ($videoList) {
            foreach ($videoList as $key => $advert) {
                $adv['adId'] = intval($advert['interface_banner_id']);
                $adv['zoneId'] = intval($advert['zone_id']);
                $adv['picUrl'] = $advert['picture'];
                $adv['adUrl'] = self::getAdUrl($advert['interface_banner_id'], $advert['zone_id'], $advert['url']);
                $adv['title'] = $advert['title'];
                $adv['imageUrl'] = $advert['imageUrl'];
                $adv['floatVideo'] = $advert['floatVideo'];
                $adv['type'] = $advert['type'];
                array_push($retAdvert, $adv);
            }
        }
        return $retAdvert;
    }

    private static function getAdUrl($adid, $zoneId, $url)
    {
        if (empty($url)) {
            return null;
        }
        return sprintf('http://ad.haibao.cn/ck.php?oaparams=2__bannerid=%s__zoneid=%s__cb=%s__oadest=%s', $adid, $zoneId, uniqid(), urlencode($url));
    }

    private static function getBmsPvMonitor($adid, $clientid, $zoneId)
    {
        return sprintf('http://ad.haibao.cn/lg.php?zones=%s,%s&cb=%s', $adid, $zoneId, uniqid());
    }
}